/**
 * Class containing the Gene strategy.
 * @author	081028AW
 */
// The chromosome is hard coded so professor Wu can run this strategy without any
// additional files. The Chromosome was evolved using the params below.
//
//#Experiment ID                :  prisoner
//#Problem Type                 :  PD
//#Data Input File Name         :  NA
//#Number of Runs               :  100
//#Generations per Run          :  100
//#Population Size              :  100
//#Selection Method             :  2
//#Fitness Scaling Type         :  0
//#Min or Max Fitness           :  max
//#Crossover Type               :  3
//#Crossover Rate               :  1.0
//#Mutation Type                :  1
//#Mutation Rate                :  0.001
//#Random Number Seed           :  75982
//#Number of Genes/Points       :  1
//#Size of Genes                :  70
//#Number of individuals in Game:  15
//#Type of Strategy             :  2
//
// It is the from last generation instead of best overall because first
// generation contains best overall due to a random population which is not 
// competitive producing usually producing at least one individual that scores
// overly high but would not be competitive in the more advanced group of 
// individuals in the last generation. We believe the best individual maybe 
// contained in a generation before the last, but that they would be very closely
// related. It would be better to determine when dip occurs and save best overall
// individual after the bottom of dip in best performance.
//
// Last Generation best Over All chromo 
//Fitness: 552.2029854910714
//#1100011000011110010110001111011000111001100100111101100110101111011111
//        #   RawFitness:  1693871051          552
public class StrategyHRTRandomK extends Strategy
   {
  /**
   * Encoding for Gene strategy.
   */

   int numDefects;
   static int LengthExpected = 70;
   String previousMoves;
   String gene;
   boolean firstMove = true;

  // 0 = defect, 1 = cooperate

   public StrategyHRTRandomK()
      {
      name = "Gene";
      gene = "1100011000011110010110001111011000111001100100111101100110101111011111";
      opponentLastMove = 1;
      numDefects = 0;
      if (gene.length() != LengthExpected)
      {
    	  //error rcm
      }
      }  /* StrategyGene */

   public int nextMove()
      {
	   if (firstMove)
	   {
		   firstMove = false;
		   previousMoves = gene.substring(64);
	   }
	   else
	   {
		   previousMoves += opponentLastMove;
	   }
	
	   int previousMovesIndex = Integer.parseInt(previousMoves, 2);
	  
	   String move = gene.substring(previousMovesIndex, previousMovesIndex+1);
	  
	   int nextMove = Integer.parseInt(move);
	   
	   String previous2Moves = previousMoves.substring(2);
	   previousMoves = previous2Moves + nextMove;
	  
	   return nextMove;
      }  /* nextMove */

   }  /* class StrategyHRTRandomK */
